﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiteFramework.Server
{
    public class SystemLog
    {
        private static string _source = null;
        private static string _logName = null;

        public static string Source
        {
            get
            {
                return (!String.IsNullOrEmpty(_source) ? _source : AppDomain.CurrentDomain.FriendlyName);
            }
            set { _source = value; }
        }

        public static string LogName
        {
            get { return (!String.IsNullOrEmpty(_logName) ? _logName : "Application"); }
            set { _logName = value; }
        }

        public static EventLog GetEventLog()
        {
            return GetEventLog(Source, LogName);
        }

        public static EventLog GetEventLog(string source)
        {
            return GetEventLog(source, null);
        }

        public static EventLog GetEventLog(string source, string logName)
        {
            if (String.IsNullOrEmpty(source))
            {
                source = Source;
            }

            if (!EventLog.SourceExists(source))
            {
                if (String.IsNullOrEmpty(logName))
                    logName = LogName;

                EventSourceCreationData sourceData = new EventSourceCreationData(source, logName);
                EventLog.CreateEventSource(sourceData);
            }

            EventLog log = new EventLog();

            log.Source = source;
            if (!String.IsNullOrEmpty(logName))
                log.Log = logName;

            log.EnableRaisingEvents = false;

            return log;
        }
    }
}
